package com.glsc.ngateway.platform.controller.itflow.cmdb;

import cn.hutool.core.util.StrUtil;
import com.glsc.ngateway.common.api.flowable.dto.FlowDoTaskRespDto;
import com.glsc.ngateway.common.api.flowable.dto.FlowFormDeviceInDetailDto;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.cmdb.CMDBCreateParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.cmdb.CMDBFindByIdParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.cmdb.CMDBPassParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.cmdb.CMDBRejectParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.cmdb.DeviceInTaskFormDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.cmdb.domainDto.FlowFormDeviceInDto;
import com.glsc.ngateway.common.api.flowable.resp.FlowableResponse;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.flowable.resp.ResponseMsgDto;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceDetail;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowEnum;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.platform.service.cmdb.CmdbDeviceDetailService;
import com.glsc.ngateway.platform.service.itflow.ItFlowService;
import com.glsc.ngateway.platform.utils.RequestTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_CREATE;

/**
 * @author weizt
 * 设备入库流程
 */
@RestController
@RequestMapping(value = "/it/cmdb/deviceIn")
@Api(value = "cmdb-设备入库流程接口", tags = {"流程-cmdb设备入库流程接口"})
public class FlowDeviceInController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ItFlowService itFlowService;

    @Resource
    private RequestTool requestTool;

    @Resource
    private CmdbDeviceDetailService cmdbDeviceDetailService;

    /**
     * 流程发起
     *
     * @param param 流程创建表单数据
     */
    @ApiOperation(value = "流程发起", notes = "流程发起")
    @PostMapping(value = "/createFlow")
    public PlatformResponse<FlowDoTaskRespDto> createFlow(@RequestBody CMDBCreateParamDto<FlowFormDeviceInDto, DeviceInTaskFormDto> param) throws Exception {
        String operatorStart = requestTool.getAccountFromRequest();
        checkArgument(param.getFlowableFormData().getDeviceInDataList());
        FlowDoTaskRespDto respDto = itFlowService.createFlow(param, operatorStart, ITFlowEnum.CMDB_DEVICE_IN.getKey());
        return PlatformResponse.successData(respDto);
    }

    private void checkArgument(List<FlowFormDeviceInDetailDto> deviceStandardNewDataList) {
        for (FlowFormDeviceInDetailDto detail:deviceStandardNewDataList) {
            if (StrUtil.isNotEmpty(detail.getSn())) {
                cmdbDeviceDetailService.findSame(CmdbDeviceDetail.builder().sn(detail.getSn()).build(), CMDB_CREATE);
            }
        }
    }

    /**
     * 保存节点数据，但是不提交任务
     */
    @ApiOperation(value = "保存节点数据", notes = "保存节点数据")
    @PostMapping("/saveFormData")
    public PlatformResponse<FlowDoTaskRespDto> saveFormData(@RequestBody CMDBCreateParamDto<FlowFormDeviceInDto, DeviceInTaskFormDto> param) throws Exception {
        String operatorStart = requestTool.getAccountFromRequest();
        FlowDoTaskRespDto respDto = itFlowService.saveFormData(param, operatorStart, ITFlowEnum.CMDB_DEVICE_IN.getKey());
        return PlatformResponse.successData(respDto);
    }

    /**
     * 审核通过
     */
    @ApiOperation(value = "审核通过", notes = "流程审核处理通过")
    @PostMapping("/taskAuditPass")
    public PlatformResponse<FlowDoTaskRespDto> taskAuditPass(@RequestBody CMDBPassParamDto<FlowFormDeviceInDto, DeviceInTaskFormDto> param) throws Exception {
        String operatorStart = requestTool.getAccountFromRequest();
        FlowDoTaskRespDto respDto = itFlowService.taskAuditPass(param, operatorStart, ITFlowEnum.CMDB_DEVICE_IN.getKey());
        return PlatformResponse.successData(respDto);
    }

    /**
     * 审核驳回
     */
    @Deprecated
    @ApiOperation(value = "审核驳回，返回至起始节点", notes = "流程审核驳回处理")
    @PostMapping("/taskAuditReject")
    public PlatformResponse<FlowableResponse<ResponseMsgDto>> taskAuditReject(@RequestBody CMDBRejectParamDto taskDto) throws PlatformException {
        FlowableResponse<ResponseMsgDto> respDto = itFlowService.taskAuditReject(taskDto, requestTool.getAccountFromRequest(), ITFlowEnum.CMDB_DEVICE_IN.getKey());
        return PlatformResponse.successData(respDto);
    }

    /**
     * 根据流程实例ID或者任务ID检索流程
     */
    @ApiOperation(value = "根据流程实例ID或者任务ID检索流程", notes = "根据流程实例ID或者任务ID检索流程状态、备注等，如提供流程实例ID则认为只读方式获取数据")
    @PostMapping("/findByProcessIdOrTaskId")
    public PlatformResponse<CMDBCreateParamDto<FlowFormDeviceInDto, DeviceInTaskFormDto>> findByProcessIdOrTaskId(@RequestBody CMDBFindByIdParamDto param) {
        CMDBCreateParamDto<FlowFormDeviceInDto, DeviceInTaskFormDto> context = new CMDBCreateParamDto<>();
        itFlowService.getWorkFlowFormByTaskId(param, ITFlowEnum.CMDB_DEVICE_IN.getKey(), context);
        return PlatformResponse.successData(context);
    }


//    @ApiOperation(value = "流程分页查询", notes = "流程分页查询")
//    @GetMapping("/findProcessPage")
//    public PlatformResponse<Page<ResponseSituationDto>> getProcessSituationPage(
//            @ApiParam(value = "流程查询参数") @RequestBody SystemFlowSearchParamDto param) {
//        String operatorStart = requestTool.getAccountFromRequest();
//        Page<ResponseSituationDto> page = itFlowService.getProcessSituationPage(param, operatorStart, ITFlowEnum.CMDB_DEVICE_IN.getKey());
//        return PlatformResponse.successData(page);
//    }
}
